package com.smartleanx.module.hrm.controller.admin.examine.examineResult;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.object.BeanUtils;
import static com.smartleanx.framework.common.pojo.CommonResult.success;

import com.smartleanx.framework.excel.core.util.ExcelUtils;

import com.smartleanx.framework.operatelog.core.annotations.OperateLog;
import static com.smartleanx.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineResult.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResult.ExamineResultDO;
import com.smartleanx.module.hrm.service.examine.examineResult.ExamineResultService;

@Tag(name = "管理后台 - 考核结果模版")
@RestController
@RequestMapping("/hrm/examine-result")
@Validated
public class ExamineResultController {

    @Resource
    private ExamineResultService examineResultService;

    @PostMapping("/create")
    @Operation(summary = "创建考核结果模版")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:create')")
    public CommonResult<Integer> createExamineResult(@Valid @RequestBody ExamineResultSaveReqVO createReqVO) {
        return success(examineResultService.createExamineResult(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考核结果模版")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:update')")
    public CommonResult<Boolean> updateExamineResult(@Valid @RequestBody ExamineResultSaveReqVO updateReqVO) {
        examineResultService.updateExamineResult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考核结果模版")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:delete')")
    public CommonResult<Boolean> deleteExamineResult(@RequestParam("id") Integer id) {
        examineResultService.deleteExamineResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考核结果模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:query')")
    public CommonResult<ExamineResultRespVO> getExamineResult(@RequestParam("id") Integer id) {
        ExamineResultDO examineResult = examineResultService.getExamineResult(id);
        return success(BeanUtils.toBean(examineResult, ExamineResultRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考核结果模版分页")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:query')")
    public CommonResult<PageResult<ExamineResultRespVO>> getExamineResultPage(@Valid ExamineResultPageReqVO pageReqVO) {
        PageResult<ExamineResultDO> pageResult = examineResultService.getExamineResultPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExamineResultRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考核结果模版 Excel")
    @PreAuthorize("@ss.hasPermission('hrm:examine-result:export')")
    @OperateLog(type = EXPORT)
    public void exportExamineResultExcel(@Valid ExamineResultPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExamineResultDO> list = examineResultService.getExamineResultPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考核结果模版.xlsx", "数据", ExamineResultRespVO.class,
                        BeanUtils.toBean(list, ExamineResultRespVO.class));
    }

}